Thread: Question about conditional operators ( || vs |)

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    73

    Question about conditional operators ( || vs |)

    I have a little question regarding this..
    I will give an example: let's say i have this condition
    Code:
    int x = 5;
    if(x ==2 || x ==5 || x ==1 || x ==8 || ....){
         return 1;
    }
    Here I pretty much sure (correct me if I am wrong) that the code will stop at: x == 5 and will return 1, and will not continue to check all the rest of the numbers.

    But when i use the operator | instead, like this:
    Code:
    unsigned short y = 316; // sorry for not using Hex number 
    unsigned short mask = 1;
    if(((y <<1) & mask) | ((y << 2)& mask) | ((y >> 1) & mask) ...){
         return 1;
    }
    let's assume that the first check will result in 1 (e.g. true) will the code stop and return 1 immediately like in the first example or it will continue to check until the end?
    Thx!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    There is no short-circuit left-to-right evaluation with the | operator.
    The compiler has to evaluate the whole expression, and it's free to do so in any order it chooses that is compatible with the precedence of the operators involved.

    So for example, the sub-expression ((y >> 1) & mask) may be evaluated before ((y << 2)& mask)

    Your analysis of the || case is correct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by patishi
    Here I pretty much sure (correct me if I am wrong) that the code will stop at: x == 5 and will return 1, and will not continue to check all the rest of the numbers.
    Yes, you are correct.

    Quote Originally Posted by patishi
    let's assume that the first check will result in 1 (e.g. true) will the code stop and return 1 immediately like in the first example or it will continue to check until the end?
    It will continue to check until the end. Note that the order of evaluation is unspecified, so the "first check" could well be ((y << 2) & mask) and "the end" could be ((y << 1) & mask), or perhaps all the shifts are evaluated first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boolean operators question
    By sfortunato in forum C Programming
    Replies: 43
    Last Post: 07-30-2011, 09:55 PM
  2. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  3. Conditional Compile Question
    By nobbyv in forum C Programming
    Replies: 1
    Last Post: 12-05-2005, 10:19 AM
  4. Question about operators.
    By Liger86 in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2002, 04:00 PM
  5. Question about logical operators
    By JohnMayer in forum C Programming
    Replies: 3
    Last Post: 07-22-2002, 06:32 PM